<?php
declare (strict_types=1);

namespace app\exam\model\admin;

use app\common\model\BaseModel;
use app\exam\event\subscribes\PaperTopicLink as PaperTopicLinkSubscribe;
use think\facade\Event;
use think\Model;

/**
 * Class PaperParts
 *
 * @package app\exam\model\admin
 */
class PaperTopicLink extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_paper_topic_link';
    /**
     * 类型转换
     *
     * @var array
     */
    protected $type = [
        'paper_id' => 'integer',
        'topic_id' => 'integer',
    ];

    public static function onAfterInsert(Model $model): void
    {
        if (!Event::hasListener('PaperTopicLinkCreated')) {
            Event::subscribe(PaperTopicLinkSubscribe::class);
        }
        Event::trigger('PaperTopicLinkCreated', $model);
    }

    public static function onAfterDelete(Model $model): void
    {
        if (!Event::hasListener('PaperTopicLinkDelete')) {
            Event::subscribe(PaperTopicLinkSubscribe::class);
        }
        Event::trigger('PaperTopicLinkDelete', $model);
    }

    /**
     * 关联试卷信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/18
     *
     * @return \think\model\relation\BelongsTo
     */
    public function paper()
    {
        return $this->belongsTo(Paper::class, 'paper_id')->bind(['paper_title' => 'title', 'update_time']);
    }

}